<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="领用单号" prop="assetCodeRule">
        <el-input v-model="queryParams.assetCodeRule" placeholder="请输入领用单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="eamList" @selection-change="handleSelectionChangeFull">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="manageId" v-model="form.manageId" />
      <el-table-column label="单据状态" align="center" prop="auditState">
        <template slot-scope="scope">
          <button class="but1" v-if="scope.row.auditState == 1">待审核</button>
          <button class="but2" v-if="scope.row.auditState == 2">已拒绝</button>
          <button class="but3" v-if="scope.row.auditState == 3">已审核</button>
          <button class="but4" v-if="scope.row.auditState == 4">已撤销</button>
        </template>
      </el-table-column>
      <el-table-column label="领用单号" align="center" :width="230" prop="assetCodeRule">
      </el-table-column>
      <el-table-column label="领用人" align="center" prop="user.nickName" width="120">
      </el-table-column>
      <el-table-column label="领用部门" align="center" prop="dept.deptName" width="100" />
      <el-table-column label="领用日期" align="center" prop="claimingDate" width="100" />
      <el-table-column label="存放位置" align="center" prop="eamLocation.locationName" />
      <el-table-column label="制单人" align="center" prop="prepared" />
      <el-table-column label="制单时间" align="center" prop="preparedDate" width="100">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.auditState != 1  || roles == 'common'"  size="mini" type="text"
            @click="audit(scope.row)">审核</el-button>

          <el-button :disabled="scope.row.auditState != 3" size="mini" type="text"
            @click="assetReturn(scope.row)">退还</el-button>
          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text"  @click="handleDelete(scope.row)">删除</el-button>

          <el-dialog title="提示" :visible.sync="reveal" width="30%" v-show="reveal">
            审核原因:<el-input v-model="Person"></el-input>
            <span slot="footer" class="dialog-footer">
              <el-button @click="reveal = false">取 消</el-button>
              <el-button type="primary" style="background-color: orange; border: 1px solid orange;"
                @click="pass('false')">不通过</el-button>
              <el-button type="primary" @click="pass('true')">通过</el-button>
            </span>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />





    <!-- 添加或修改资产记录2对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body style="margin-left: 150px;">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="领用部门">
              <el-select placeholder="请选择" value="请选择" v-model="lowerCond.dept_Id" @change="getUserId">
                <el-option v-for="item in lowerBranchList" :key="item.deptId" :value="item.deptId"
                  :label="item.deptName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领用人员">
              <el-select placeholder="请选择" value="请选择" v-model="form.usePerson">
                <el-option v-for="item in lowerStaffList" :key="item.userId" :value="item.userId"
                  :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="存放位置">
              <el-select placeholder="请选择" value="请选择" v-model="formd.locationId">
                <el-option v-for="item in EntrepotList" :key="item.locationId" :value="item.locationId"
                  :label="item.locationName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领用日期">
              <el-date-picker type="date" placeholder="选择日期" v-model="form.claimingDate">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="备注">
              <el-input v-model="form.remark">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div class="box_scope">
              <el-button v-show="isAdd" style="margin-left: 15px;" type="" plain size="mini"
                @click="add">新增<strong></strong></el-button>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="div-yj">
              <div>
                <el-table :data="assetrkList" border>
                  <el-table-column label="序号" align="center" prop="assetId" />
                  </el-table-column>

                  <el-table-column label="选择资产" align="center" prop="assetCodeRule" :min-width="150">
                    <!-- 使用作用域插槽自定义列内容 -->
                    <template #default="scope">
                      <!-- 在作用域插槽内插入文本框 -->
                      <img @click="zxasset()" class="img1" src="../../../assets/image/搜索.png" />
                      <el-input v-model="scope.row.assetCodeRule"></el-input>
                    </template>
                  </el-table-column>
                  <el-table-column label="资产名称" align="center" prop="assetName" /></el-table-column>
                  <el-table-column label="数量" align="center" prop="num" /></el-table-column>
                  <el-table-column label="金额" align="center" prop="price" /></el-table-column>
                  <el-table-column label="规格" align="center" prop="asseSpec" width="280" /></el-table-column>
                  <el-table-column label="型号" align="center" prop="model"></el-table-column>
                  <el-table-column label="报废日期" align="center" prop="scrapTime" /></el-table-column>

                </el-table>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="open2" width="1000px" append-to-body>
      <el-row>
        <el-col :span="24">
          <el-input placeholder="资产名称" maxlength="30" style="width: 200px;" v-model="eamCond.assetName" />&nbsp;
          <el-select placeholder="请选择" value="全部资产类型" v-model="eamCond.TypeId">
            <el-option value="0" label="请选择"></el-option>
            <el-option v-for="item,index in eamTypeList" :key="index" :value="item.typeId"
              :label="item.typeName"></el-option>
          </el-select>&nbsp;
          <el-select placeholder="请选择" value="全部存放位置" v-model="eamCond.locationId">
            <el-option value="0" label="请选择"></el-option>
            <el-option v-for="item,index in EntrepotList" :key="index" :value="item.locationId"
              :label="item.locationName"></el-option>
          </el-select>&nbsp;
          <el-button type="primary" icon="el-icon-search" size="mini" @click="selectIdleEam"
            style="height: 34px;">查询</el-button>
          <el-table :data="assetyjList" border style="padding-top: 10px;" @selection-change="handleSelectionChange"
            ref="uniqueIdentifier">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="资产名称" align="center" prop="assetName" />
            <el-table-column label="资产编码" align="center" prop="assetCodeRule" />
            <el-table-column label="资产类型" align="center" prop="eamType.typeName" />
            <el-table-column label="规格" align="center" prop="asseSpec" />
            <el-table-column label="型号" align="center" prop="model" />
            <el-table-column label="资产金额(元)" align="center" prop="price" />
            <el-table-column label="报废日期" align="center" prop="scrapTime" />
            <el-table-column label="数量" align="center" prop="assetNum">
              <template slot-scope="scope">
                <div v-if="!isAdd"><el-input-number v-model="scope.row.num" :min="1"
                    :max="parseInt(scope.row.totalAsset)" @change="handleChange" label="描述文字"></el-input-number>
                </div>
                <div v-if="isAdd"><el-input-number v-model="scope.row.num" :min="1" :max="scope.row.assetNum"
                    @change="handleChange" label="描述文字"></el-input-number>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[3,4,5,6]"
        :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="pageContent.total"
        @pagination="selectIdleEam">
      </el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isOpen">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>




    <el-dialog :title="title3" :visible.sync="open3" width="1200px" append-to-body style="margin-left: 150px;">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <div class="div-yj">
              <div>
                <el-table :data="assetrkList" border>
                  <el-table-column label="序号" align="center" prop="assetId" width="100" />
                  <el-table-column label="资产名称" align="center" prop="assetName" width="120" /></el-table-column>
                  <el-table-column label="规格" align="center" prop="asseSpec" width="160" /></el-table-column>
                  <el-table-column label="型号" align="center" prop="model"></el-table-column>
                  <el-table-column label="领用数量" align="center" prop="count" width="80" /></el-table-column>
                  <el-table-column label="退还数量" align="center">
                    <template slot-scope="scope">
                      <div>
                        <el-input-number v-model="scope.row.num" :min="1" :max="scope.row.count" @change="handleChange"
                          label="描述文字"></el-input-number>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column label="备注" align="center">
                    <template slot-scope="scope">
                      <div>
                        <el-input v-model="scope.row.remark" style="width: 150px;"></el-input>
                      </div>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addReturnAsset">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {
    listEam,
    getEam,
    delEam,
    addEam,
    updateEam,
    listLowerBranch,
    listLowerBranchdata,
    listLowerStaff,
    listEntrepot,
    listIdleEam,
    listType,
    addEamD,
    getCode,
    updDel,
    updDelEamD,
    updateEamD,
    updateAsset,
    getReturnCode,
    addH,
    addD
  } from "@/api/eam-xs/eam";
  import Cookies from "js-cookie";
  import {
    getInfo
  } from "@/api/login";

  export default {
    name: "Eam",
    data() {
      return {
        // 遮罩层
        loading: true,
        //用户选择资产数量
        num: 1,
        title3: "",
        open3: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        Person: "",
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 资产记录2表格数据
        eamList: [],
        // 弹出层标题
        title: "",
        reveal: false,
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        assetrkList: [],
        pageContent: {
          total: ''
        },
        //领用部门下拉框数据
        lowerBranchList: {},
        //领用人员下拉框数据
        lowerStaffList: {},
        //仓库数据
        EntrepotList: {},
        //根据用户选择下拉框获取对应数据条件
        lowerCond: {
          dept_Id: "",
          user_Id: "",
          locationId: "",
          claimingDate: "",
        },
        //根据资产状态查询资产条件
        eamCond: {
          useStatus: 4,
          locationId: "",
          TypeId: "",
          assetName: "",
          number: 1
        },
        //选中添加资产数据
        eamInsertList: {
          number: "",
          assetName: "",
          assetCode: "",
          price: "",
          asseSpec: "",
          model: "",
          scrapTime: ""
        },
        //闲置资产数据
        assetyjList: [],
        //资产类型数据
        eamTypeList: [],
        title2: "",
        open2: false,
        // eam_manage_h表单参数
        form: {},
        // eam_manage_d表单参数
        formd: {
          assetId: null,
          manageId: null,
          count: 0,
          locationId: null,
          isdel: 1
        },
        //eam_return_h表单参数
        formReturnH: {
          returnId: null,
          audiStatus: 1,
          returnCode: null,
          returnTime: null,
          usePerson: null,
          remark: null,
          preparedDate: null,
          prepared: null,
          isdel: 1
        },
        //eam_return_d表单参数
        formReturnD: {
          assetId: null,
          manageId: null,
          count: null,
          locationId: null,
          isdel: 1
        },
        // 表单校验
        rules: {},
        selectedRows: [],
        codeRule: [],
        manageId: [],
        manage: [],
        isAdd: "",
        length: 0,
        addisUpd: 0, //1是新增2是修改
        code: {},
        item: {},
        totalAsset: {},
        selectCheckList: [],
        passList: {},
        returnList: [],
        roles:undefined,
        user:{},
      };
    },
    created() {
      this.getList();
      this.selectIdleEam();
      this.selectEntrepot();
      this.selectType();
      getInfo().then((res) => {
        this.user = res.user;
        this.roles = res.roles;
      });

    },
    watch: {},
    methods: {
      getTotalNumber(row) {
        let num = row.num;
        this.total = row.assetNum + num;
      },

      getUserId() {
        listLowerBranchdata(this.lowerCond.dept_Id).then(response => {
          this.lowerStaffList = response.data;
        })
      },

      /** 查询资产记录2列表 */
      getList() {
        this.loading = true;
        listEam(this.queryParams).then(response => {
          console.log("eam" + JSON.stringify(response));
          this.eamList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.log("eamlist" + JSON.stringify(this.eamList));
        });

        //获取领用部门下拉框数据
        listLowerBranch().then(response => {
          this.lowerBranchList = response.data;
        })

        //获取领用人员下拉框数据
        listLowerStaff().then(response => {
          this.lowerStaffList = response.rows;
        })

      },
      handleChange(value) {

      },
      //查询仓库
      selectEntrepot() {
        listEntrepot().then((response) => {
          this.EntrepotList = response.rows;
        })
      },
      //查询类型
      selectType() {
        listType().then((response) => {
          this.eamTypeList = response.rows;
        })
      },
      //点击确定关闭open2
      isOpen() {
        this.open2 = false;
        if (this.assetrkList.length > 1) {
          for (let i = 0; i < this.assetrkList.length; i++) {
            if (i != 0) {
              this.selectCheckList.push(this.assetrkList[i])
            }
          }
        }
      },
      //查询所有闲置资产
      selectIdleEam() {
        this.eamCond.locationId = this.eamCond.locationId == 0 ? "" : this.eamCond.locationId;
        this.eamCond.TypeId = this.eamCond.TypeId == 0 ? "" : this.eamCond.TypeId;
        listIdleEam(this.eamCond).then((response) => {
          this.assetyjList = response.rows;
          this.pageContent.total = response.total;
          this.assetyjList = this.assetyjList.map(item => ({
            ...item, // 使用展开语法保留原始对象的所有属性
            num: 1, // 添加 num 字段并设置初始值为 1
            assetCodeRule: item.assetCode,
          }));
        })

      },
      // 表单重置
      reset() {
        this.form = {
          manageId: null,
          assetCode: null,
          prepared: null,
          preparedDate: null,
          remark: null,
          receiptsStatus: 1,
          auditState: 1,
          creator: 2,
          createTime: null,
          auditPerson1: null,
          claimingDate: null,
          assetCodeRule: null,
          usePerson: null,
          isdel: 1,
          num: 0,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      handleCurrentChange(val) {

      },
      handleSizeChange(val) {
        this.selectIdleEam();
      },
      cancel2() {
        this.open2 = false;
        this.reset();
      },
      cancel() {
        this.open = false;
        this.open3 = false;
        this.formd.locationId = undefined;
        this.reset();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        for (let i = 0; i < selection.length; i++) {
          if (this.codeRule != "") {
            selection[i].assetCode = (this.codeRule[i].msg);
          }
        }
        this.ids = selection.map(item => item.manageId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
        if (selection != "") {
          this.assetrkList = selection;
        }
      },
      handleSelectionChangeFull(seletion) {
        for (let i = 0; i < seletion.length; i++) {
          let assetCodeRule = seletion[i].assetCodeRule;
          let manageId = seletion[i].manageId;
          if (this.manage.length > seletion.length) {
            this.manage = [];
            if (!this.manage.includes(assetCodeRule)) {
              this.manage.push(assetCodeRule);
            }
          } else {
            if (!this.manage.includes(assetCodeRule)) {
              this.manage.push(assetCodeRule);
            }
          }
          if (this.manageId.length > seletion.length) {
            this.manageId = [];
            if (!this.manageId.includes(manageId)) {
              this.manageId.push(manageId)
            }
          } else {
            if (!this.manageId.includes(manageId)) {
              this.manageId.push(manageId)
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.isAdd = true;
        this.addisUpd = 1;
        this.title = "添加领用记录";
        this.assetrkList = []
        this.lowerCond.locationId = "";
        this.lowerCond.dept_Id = "";
        this.lowerCond.user_Id = "";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.code = row.assetCodeRule;
        this.addisUpd = 2;
        this.assetrkList = [];
        this.open = true;
        this.isAdd = false;
        this.title = "修改领用记录";
        this.lowerCond.dept_Id = row.dept.deptId;
        this.form.usePerson = row.usePerson;
        this.formd.locationId = row.eamLocation.locationId;
        this.form.remark = row.remark;
        this.form.claimingDate = row.claimingDate;
        this.form.manageId = row.manageId;
        let obj = {};
        obj.num = row.eamManageD.count;
        this.item = obj;
        this.assetrkList.push({
          "assetId": row.eamAsset.assetId,
          "assetCodeRule": row.assetCodeRule,
          "assetName": row.eamAsset.assetName,
          "num": row.eamManageD.count,
          "price": row.eamAsset.price,
          "asseSpec": row.eamAsset.asseSpec,
          "model": row.eamAsset.model,
          "scrapTime": row.eamAsset.scrapTime
        });
        for (let i = 0; i < this.assetyjList.length; i++) {
          if (this.assetyjList[i].assetId == row.eamAsset.assetId) {
            this.assetyjList[i].num = row.eamManageD.count;
            this.selectCheckList.push(
              this.assetyjList[i]
            )
            setTimeout(() => {
              this.$refs.uniqueIdentifier.clearSelection();
              this.$refs.uniqueIdentifier.toggleRowSelection(this.assetyjList[i], true);
            }, 3000)
          } else {
            this.assetyjList[i].num = 1;
          }
        }

      },
      /** 提交按钮 */

      submitForm() {
        // if(this.assetrkList[0].assetCode == null){
        //   this.$message.warning("请选择需要领用的资产")
        //   return;
        // }
        let msg = null;
        let msgFull = null;
        if (this.addisUpd == 1) {
          if(this.assetrkList.length != 0){
            if(this.assetrkList[0].assetCode == null){
              this.$message.warning("请选择需要领用的资产")
              return;
            }
          }
          this.assetrkList.forEach((item) => {
            this.form.assetCode = item.assetCode;
            this.form.assetCodeRule = item.assetCode;
            this.form.preparedDate = this.form.claimingDate;
            this.form.prepared = Cookies.get("username");
            this.formd.assetId = item.assetId;
            this.formd.manageId = item.assetCode;
            this.formd.count = item.num;
            if (!this.lowerCond.dept_Id == "" || this.form.usePerson == "" || this.formd.locationId == "" || this
              .form.claimingDate == "" || this.assetrkList == "") {
              addEam(this.form).then(response => {
                msg = response.code;
              })
              addEamD(this.formd).then(response => {
                msgFull = response.code;
              })
              let asset = {};
              asset.assetId = item.assetId;
              asset.assetNum = item.num;
              updateAsset(asset).then((res) => {})
            }
          })
          if (msg == msgFull && this.lowerCond.dept_Id != "" && this.form.usePerson != "" && this.formd.locationId !=
            "" && this
            .form.claimingDate != "" && this.assetrkList != "") {
            this.$modal.msgSuccess("添加成功");
            this.formd.locationId = undefined;
            this.open = false;
            setTimeout(() => {
              this.getList();
              this.selectIdleEam();
              this.$refs.uniqueIdentifier.clearSelection();
            }, 3000)
          } else {
            this.$message({
              message: '信息填写不完整!',
              type: 'warning'
            });
          }

        } else {        //修改
          this.assetrkList.forEach((item,index) => {
            const it = this.assetrkList[0];
            this.form.assetCode = item.assetCode;
            this.form.assetCodeRule = this.code;
            this.form.preparedDate = this.form.claimingDate;
            this.form.prepared = Cookies.get("username");
            this.formd.assetId = it.assetId;
            this.formd.manageId = item.assetCode;
            this.formd.count = item.num;
            this.num = item.num;
          });
          if(this.roles == "common"){
            if(this.user.userId != this.form.prepared){
              this.$message.warning("您没有此权限");
              return;
            }
          }

          if (this.assetrkList.length > 1) {
            for (let i = 0; i < this.selectCheckList.length; i++) {
              if (this.selectCheckList[i].assetId != this.formd.assetId) {
                //如果修改的时候添加了多个资产
                let randomNum = "LYD" + Math.floor(Math.random() * 10000000000000000);

                this.form.assetCode = randomNum;
                this.form.assetCodeRule = randomNum;
                this.form.preparedDate = this.form.claimingDate;
                this.form.receiptsStatus = 1;
                this.form.auditState = 1;
                this.form.creator = 2;
                this.form.isdel = 1;
                this.form.prepared = Cookies.get("username");
                this.formd.manageId = randomNum;
                this.formd.count = this.selectCheckList[i].num;
                this.formd.assetId = this.selectCheckList[i].assetId;
                this.form.manageId = null;
                this.addBatch();
                let asset = {};
                asset.assetId = this.selectCheckList[i].assetId;
                asset.assetNum = this.selectCheckList[i].num;
                updateAsset(asset).then((res) => {})
              }
            }
          } else if (this.assetrkList.length == 1) { //如果是单独修改资产
            for (let i = 0; i < this.selectCheckList.length; i++) {
              if (this.selectCheckList[i].assetId != this.assetrkList[i].assetId) {

                let upd, updFull = null;
                let number = this.selectCheckList[i].totalAsset - 1;
                let obj = {};

                //把原来的资产数量调整回去
                obj.assetId = this.selectCheckList[i].assetId;
                obj.incrementValue = this.selectCheckList[i].num;

                updateAsset(obj).then((response) => {})
                this.selectCheckList[i] = this.assetrkList[i];
                //减少
                let flag = this.num;
                obj = {};
                obj.assetId = this.selectCheckList[0].assetId;
                obj.assetNum = flag;

                updateAsset(obj).then((response) => {})

                delete this.form.assetCode;
                updateEam(this.form).then((res) => {
                  upd = res.code;
                })
                this.formd.manageId = this.code;
                updateEamD(this.formd).then((res) => {
                  updFull = res.code;
                })
                if (upd == updFull) {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  setTimeout(() => {
                    this.getList();
                    this.selectIdleEam();
                    this.$refs.uniqueIdentifier.clearSelection();
                  }, 3000)
                }
                //-----------------------------------------------------------------------------------------------------------------------------------------
              } else { //修改原来资产的信息而不是资产
                let upd, updFull = null;
                let number = this.item.num;
                let obj = {};

                if (this.num > number) {
                  //新增
                  let flag = this.num - number;
                  obj.assetId = this.formd.assetId;
                  obj.assetNum = flag;
                  updateAsset(obj).then((response) => {})
                } else {
                  //减少
                  let flag = number - this.num;
                  obj.assetId = this.formd.assetId;
                  obj.incrementValue = flag;
                  updateAsset(obj).then((response) => {})
                }
                delete this.form.assetCode;
                updateEam(this.form).then((res) => {
                  upd = res.code;
                })

                this.formd.manageId = this.code;
                updateEamD(this.formd).then((res) => {
                  updFull = res.code;
                })
                if (upd == updFull) {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  setTimeout(() => {
                    this.getList();
                    this.selectIdleEam();
                    this.$refs.uniqueIdentifier.clearSelection();
                  }, 3000)
                }
              }
            }
          }
        }
      },
      //添加方法执行过程
      addBatch() {
        let msg = null;
        let msgFull = null;
        addEam(this.form).then(response => {
          msg = response.code;
        })
        addEamD(this.formd).then(response => {
          msgFull = response.code;
        })
        if (msg == msgFull) {
          this.$modal.msgSuccess("添加成功");
          this.open = false;
          setTimeout(() => {
            this.getList();
            this.selectIdleEam();
            this.$refs.uniqueIdentifier.clearSelection();
          }, 3000)
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        if(this.roles == "common"){
          if(this.user.userId != row.prepared){
            this.$message.warning("您没有此权限");
            return;
          }
        }
        if (confirm("是否删除?")) {
          this.manageId = this.manageId == "" ? [row.manageId] : this.manageId;
          this.manage = this.manage == "" ? [row.assetCodeRule] : this.manage;
          let msg, msgFull = null;
          updDel(this.manageId).then((response) => {
            msg = response.code;
          })
          updDelEamD(this.manage).then((response) => {
            msgFull = response.code;
          })
          if (msg == msgFull) {
            this.$modal.msgSuccess("删除成功");
            setTimeout(() => {
              this.manageId = [];
              this.manage = [];
              this.getList();
            }, 1000)
          }
        }
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('eam-a/eam/export', {
          ...this.queryParams
        }, `eam_${new Date().getTime()}.xlsx`)
      },
      djls() {
        // this.reset();
        this.open3 = true;
        this.title3 = "单据流水";
      },
      zxasset() {
        this.assetyjList = this.assetyjList.map(item => ({
          ...item,
          totalAsset: item.num + item.assetNum,
        }));
        if (this.isAdd == true) {
          for (let i = 0; i < this.assetyjList.length; i++) {
            this.assetyjList[i].num = 1;
          }
        } else {
          for (let i = 0; i < this.assetyjList.length; i++) {
            if (this.selectCheckList[0].assetId == this.assetyjList[i].assetId) {
              setTimeout(() => {
                this.$refs.uniqueIdentifier.toggleRowSelection(this.assetyjList[i], true);
              }, 1000)
            }
          }
        }
        this.open2 = true;
        this.title2 = "选择资产";
      },
      add() {
        this.codeRule = [];
        if (this.isAdd) {
          this.assetrkList.push({})
        }
        getCode().then((response) => {
          this.codeRule.push(response);
        })
      },
      //审核
      audit(row) {
        this.Person = "";
        this.reveal = true;
        this.passList = row;
      },

      pass(flag) {
        //审核通过
        let asset = {};
        asset.manageId = this.passList.manageId;
        if (flag == "true") {
          asset.auditState = 3;
        } else {
          //审核不通过
          asset.auditState = 2;
        }
        updateEam(asset).then((res) => {
          if (res.code == 200) {
            if (flag == "true") {
              this.$modal.msgSuccess("序号为" + asset.manageId + "审核通过");
            } else {
              this.$message("序号为" + asset.manageId + "审核已拒绝");
            }
            this.getList();
          }
        })
        this.reveal = false;
      },
      //资产退还
      assetReturn(row) {
        if(this.roles == "common"){
          if(this.user.userId != row.prepared){
            this.$message.warning("您没有此权限");
            return;
          }
        }
        let This = this;
        this.queryParams.usePerson = row.usePerson;
        this.open3 = true;
        this.assetrkList = [];
        listEam(this.queryParams).then(response => {
          This.returnList = response.rows;
          for (let i = 0; i < this.returnList.length; i++) {
            this.assetrkList.push({
              "assetId": This.returnList[i].eamAsset.assetId,
              "assetCodeRule": This.returnList[i].assetCodeRule,
              "assetName": This.returnList[i].eamAsset.assetName,
              "count": This.returnList[i].eamManageD.count,
              "price": This.returnList[i].eamAsset.price,
              "asseSpec": This.returnList[i].eamAsset.asseSpec,
              "model": This.returnList[i].eamAsset.model,
              "scrapTime": This.returnList[i].eamAsset.scrapTime,
              "num": This.returnList[i].eamManageD.count,
              "remark": "",
              "locationId": This.returnList[i].eamLocation.locationId,
              "usePerson": This.returnList[i].usePerson,
              "manageId": This.returnList[i].assetCodeRule,
              "assetCode": This.returnList[i].manageId,
            });
          }
        });
      },
      addReturnAsset() {
        for (let i = 0; i < this.assetrkList.length; i++) {
          getReturnCode().then((response) => {
            this.formReturnH.returnCode = response.msg;
            this.formReturnH.usePerson = this.assetrkList[i].usePerson;
            this.formReturnH.remark = this.assetrkList[i].remark;
            this.formReturnH.prepared = this.user.userId;
            this.formReturnD.assetId = this.assetrkList[i].assetId;
            this.formReturnD.locationId = this.assetrkList[i].locationId;
            this.formReturnD.count = this.assetrkList[i].num;
            this.formReturnD.manageId = response.msg;
            this.formReturnH.returnTime = this.assetrkList[i].scrapTime;
            this.formReturnH.preparedDate = this.assetrkList[i].scrapTime;
            let asset = {};
            asset.assetId = this.assetrkList[i].assetId;
            asset.incrementValue = this.assetrkList[i].num;
            let assetD = {};
            assetD.manageId = this.assetrkList[i].manageId;
            let assetH = {};

            assetD.count = this.assetrkList[i].count - this.assetrkList[i].num;
            if (assetD.count == 0) {
              assetD.count = 0;
              assetD.isdel = 0;
              //eam_manageId_h把isdel变成0
              assetH.manageId = this.assetrkList[i].assetCode;
              assetH.isdel = 0;
              updateEam(assetH).then((response) => {})
            }
            updateAsset(asset).then((response) => {})

            updateEamD(assetD).then((response) = {})
            addH(this.formReturnH).then((res) => {})
            addD(this.formReturnD).then((response) => {
              if (response.code == 200 && i == this.assetrkList.length - 1) {
                this.$modal.msgSuccess("成功退还");
              }
            })
          });
        }
        this.queryParams.usePerson = null;
        setTimeout(() => {
          this.getList();
        }, 3000)
        this.open3 = false;
      }
    }
  }
</script>
<style>
 /* 单据状态 */
 .but1 {
   color: sandybrown;
   border-radius: 30px;
   font-size: 11px;
   background-color: #ffdcbc;
   border: 1px solid sandybrown;
 }

 .but2 {
   color: #aa3800;
   border-radius: 30px;
   font-size: 11px;
   background-color: #ff8d6a;
   border: 1px solid #ff8d6a;
 }

 .but3 {
   color: #4ab0f9;
   border-radius: 30px;
   font-size: 11px;
   background-color: #e6eff4;
   border: 1px solid #4ab0f9;
 }

 .but4 {
   color: #550000;
   border-radius: 30px;
   font-size: 11px;
   background-color: #d1d9dd;
   border: 1px solid #550000;
 }

  .box_scope img {
    width: 25px;
    height: 22px;
    float: left;
    margin-left: 10px;
  }

  .box_scope img:hover {
    cursor: pointer;
  }

  .img1 {
    height: 20px;
    position: absolute;
    z-index: 1;
    /* 设置图片的z-index值高于其他元素 */
    right: 15px;
    top: 18px;
    cursor: pointer;
    /* 将鼠标指针设置为手形 */
    transition: background-color 0.3s ease;
    /* 添加过渡效果 */
  }

  .el-input-number {
    width: 100px;
  }

  .el-form-item .el-input {
    width: 160px;
  }
</style>
